// info.js

//const config = require('../../config/config.default.js')
var app = getApp()
Page({
  data: {
    send: false,
    alreadySend: false,
    second: 60,
    disabled: true,
    buttonType: 'default',
    phoneNum: '',
    code: '',
    smscode: '',
    otherInfo: '',
    u_phone: ''
  },
  onLoad: function (options) {
    var bgc = wx.getStorageSync('color')
    var u_phone = options.u_phone
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    var that = this
    that.setData({
      bgc: bgc,
      u_phone: u_phone
    })
    var openid = wx.getStorageSync('openid');
    if (options.enter) {
      that.setData({
        enter: options.enter
      })
    }
    app.util.request({
      'url': 'entry/wxapp/hzbingli.url',
      success: function (res) {
        console.log(res)
        that.setData({
          url: res.data,
        })
      }
    })
  },

  // 手机号部分
  inputPhoneNum: function (e) {
    let phoneNum = e.detail.value
    if (phoneNum.length === 11) {
      let checkedNum = this.checkPhoneNum(phoneNum)
      if (checkedNum) {
        this.setData({
          phoneNum: phoneNum
        })
        console.log('phoneNum' + this.data.phoneNum)
        this.showSendMsg()
        this.activeButton()
      }
    } else {
      this.setData({
        phoneNum: ''
      })
      this.hideSendMsg()
    }
  },

  checkPhoneNum: function (phoneNum) {
    let str = /^1\d{10}$/
    if (str.test(phoneNum)) {
      return true
    } else {
      wx.showToast({
        title: '手机号不正确',
        image: '../../../images/error.png'
      })
      return false
    }
  },

  showSendMsg: function () {
    if (!this.data.alreadySend) {
      this.setData({
        send: true
      })
    }
  },

  hideSendMsg: function () {
    this.setData({
      send: false,
      disabled: true,
      buttonType: 'default'
    })
  },

  sendMsg: function () {
    var that = this;
    var uniacid = app.siteInfo.uniacid
    app.util.request({
      url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=SendSms&m=hyb_yl',
      data: {
        phoneNum: this.data.phoneNum
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        that.setData({
          smscode: res.data.data,
        })
      }
    })
    this.setData({
      alreadySend: true,
      send: false
    })
    this.timer()
  },

  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(
        () => {
          this.setData({
            second: this.data.second - 1
          })
          if (this.data.second <= 0) {
            this.setData({
              second: 60,
              alreadySend: false,
              send: true
            })
            resolve(setTimer)
          }
        }, 1000)
    })
    promise.then((setTimer) => {
      clearInterval(setTimer)
    })
  },

  // 其他信息部分
  addOtherInfo: function (e) {
    console.log(e)
    this.setData({
      otherInfo: e.detail.value
    })
    this.activeButton()
    console.log('otherInfo: ' + this.data.otherInfo)
  },

  // 验证码
  addCode: function (e) {
    console.log(e)
    this.setData({
      code: e.detail.value
    })
    this.activeButton()
    console.log('code' + this.data.code)
  },

  // 按钮
  activeButton: function () {
    let {
      phoneNum,
      code,
      otherInfo
    } = this.data
    console.log(otherInfo)
    if (phoneNum && code && otherInfo) {
      this.setData({
        disabled: false,
        buttonType: 'primary'
      })
    } else {
      this.setData({
        disabled: true,
        buttonType: 'default'
      })
    }
  },

  onSubmit: function (e) {
    var that = this
    var openid = wx.getStorageSync('openid')
    let myphone = this.data.phoneNum
    let code = parseInt(this.data.code)
    let smscode = this.data.smscode


    if (smscode == '' || code != smscode) {
      wx.showToast({
        title: '验证失败',
        icon: 'error'
      })
    } else {

      if (that.data.enter == 1) {
        var pages = getCurrentPages();
        var prevPage = pages[pages.length - 2];
        prevPage.setData({
          myphone: myphone
        })
        wx.navigateBack({
          delta: 1
        })
      } else {
        app.util.request({
          'url': "entry/wxapp/Savemyphone",
          data: {
            u_phone: myphone,
            openid: openid
          },
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res)
            wx.showToast({
              title: '保存成功',
              icon: 'none',
              success: function (res) {
                wx.redirectTo({
                  url: '/hyb_yl/tabBar/my/my',
                })
              }
            })
          },
          fail: function (err) {
            console.log(err)
          },
        })
      }

    }
  },

})